package com.sykj.why.controller.admin.category;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.AuxiliaryCategoryT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.AuxiliaryCategoryTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eleven
 * @date 2019/6/19
 * @desc 辅助分类控制访问层
 */

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(value = "/admin/category",description = "辅助分类控制访问层")
public class AuxiliaryCategoryController {

    @Autowired
    private AuxiliaryCategoryTService auxiliaryCategoryTService;

    /**
     * 添加辅助分类
     * @param auxiliaryCategoryT
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "/add",notes = "添加辅助分类")
    public ResponseEntity add(@RequestBody AuxiliaryCategoryT auxiliaryCategoryT){
        try {
            auxiliaryCategoryTService.saveAuxiliaryCategory(auxiliaryCategoryT);
            return Response.success("添加成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("添加辅助分类出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 删除辅助分类
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "/remove",notes = "删除辅助分类")
    public ResponseEntity remove(@PathVariable String id){
        try {
            auxiliaryCategoryTService.removeAuxiliaryCategory(id);
            return Response.success("删除成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("删除辅助分类出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 修改辅助分类
     * @param auxiliaryCategoryT
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "/update",notes = "修改辅助分类")
    public ResponseEntity update(@RequestBody AuxiliaryCategoryT auxiliaryCategoryT){
        try {
            auxiliaryCategoryTService.updateAuxiliaryCategory(auxiliaryCategoryT);
            return Response.success("修改成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("修改辅助分类出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 启用/禁用辅助分类
     * @param id
     * @param flag
     * @return
     */
    @RequestMapping(value = "/enable",method = RequestMethod.POST)
    @ApiOperation(value = "/enable",notes = "启用/禁用辅助分类")
    public ResponseEntity enable(@RequestParam String id,@RequestParam boolean flag){
        try {
            auxiliaryCategoryTService.disableOrEnable(id, flag);
            return Response.success("操作成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("启用禁用辅助分类出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 查询全部辅助分类
     *
     * @return 全部辅助分类
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value ="/findAll",notes = "查询全部辅助分类")
    public ResponseEntity findAll() {
        try {
            return Response.success("查询成功", auxiliaryCategoryTService.findAll());
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("查询全部辅助分类出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 通过父级Id查询子级分类
     * @param pid
     * @return
     */
    @RequestMapping(value = "/findByPid/{pid}",method = RequestMethod.GET)
    @ApiOperation(value ="/findByPid/{pid}",notes = "通过父级Id查询子级分类")
    public ResponseEntity findByPid(@PathVariable String pid) {
        try {
            return Response.success("查询成功", auxiliaryCategoryTService.findByPid(pid));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("通过父级Id查询子级分类出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 前端树状图
     *
     * @return
     */
    @RequestMapping(value = "/findTree",method = RequestMethod.GET)
    @ApiOperation(value ="/findTree",notes = "前端树状图")
    public ResponseEntity findTree() {
        try {
            return Response.success("查询成功", auxiliaryCategoryTService.findTree("0"));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("前端树状图出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
